# -*- coding:utf-8 -*-
#! python3

# FaceCat-Python
#Shanghai JuanJuanMao Information Technology Co., Ltd 

import math
import time
import datetime
from operator import attrgetter
import uuid
import os
from xml.etree import ElementTree as ET
from objc import * 
from Cocoa import *
from AppKit import *
from Quartz import *
from facecat_objc import *
#pip3 uninstall pyobjc
#pip3 install pyobjc

#更新悬浮状态
#views:视图集合
def updateView(views):
	updateViewDefault(views)

#绘制视图
#view:视图
#paint:绘图对象
#clipRect:区域
def onPaint(view, paint, clipRect):
	onPaintDefault(view, paint, clipRect)

#绘制视图边线
#view:视图
#paint:绘图对象
#clipRect:区域
def onPaintBorder(view, paint, clipRect):
	onPaintBorderDefault(view, paint, clipRect)

#视图的鼠标移动方法
#view 视图
#mp 坐标
#buttons 按钮 0未按下 1左键 2右键
#clicks 点击次数
#delta 滚轮值
def onMouseMove(view, mp, buttons, clicks, delta):
	onMouseMoveDefault(view, mp, buttons, clicks, delta)
		
#视图的鼠标按下方法
#view 视图
#mp 坐标
#buttons 按钮 0未按下 1左键 2右键
#clicks 点击次数
#delta 滚轮值
def onMouseDown(view, mp, buttons, clicks, delta):
	onMouseDownDefault(view, mp, buttons, clicks, delta)

#视图的鼠标抬起方法
#view 视图
#mp 坐标
#buttons 按钮 0未按下 1左键 2右键
#clicks 点击次数
#delta 滚轮值
def onMouseUp(view, mp, buttons, clicks, delta):
	onMouseUpDefault(view, mp, buttons, clicks, delta)

#视图的鼠标点击方法
#view 视图
#mp 坐标
#buttons 按钮 0未按下 1左键 2右键
#clicks 点击次数
#delta 滚轮值
def onClick(view, firstTouch, firstPoint, secondTouch, secondPoint, clicks):
	onClickDefault(view, firstTouch, firstPoint, secondTouch, secondPoint, clicks)

#视图的鼠标滚动方法
#view 视图
#mp 坐标
#buttons 按钮 0未按下 1左键 2右键
#clicks 点击次数
#delta 滚轮值
def onMouseWheel(view, mp, buttons, clicks, delta):
	onMouseWheelDefault(view, mp, buttons, clicks, delta)

appPaint = FCPaint() #创建绘图对象
appPaint.onPaint = onPaint 
appPaint.onPaintBorder = onPaintBorder
appPaint.onMouseDown = onMouseDown 
appPaint.onMouseMove = onMouseMove 
appPaint.onMouseUp = onMouseUp
appPaint.onMouseWheel = onMouseWheel
appPaint.onClick = onClick
appPaint.onUpdateView = updateView

#加载示例
def loadUI():
	xml = """<?xml version="1.0" encoding="UTF-8"?>
	<html xmlns="facecat">
	<head></head>
	<body>
		<div dock="fill" type="tab" selectedindex="0">
		<div name="TabPage" text="可视化生成" type="tabpage">
			<tree name="Tree" text="Tree" location="45,56" size="200,200" headervisible="False">
			</tree>
			<tree name="Tree2" text="Tree2" location="256,56" size="200,200">
			<tr>
				<th text="FCGridColumn" name="FCGridColumn" width="300"/>
				<th text="FCGridColumn2" name="FCGridColumn2"/>
				<th text="FCGridColumn3" name="FCGridColumn3"/>
			</tr>
			<nodes>
				<node text="Node1">
				<node text="Node2">
				<node text="Node3"/>
				</node>
				</node>
			</nodes>
			</tree>
		</div>
		<div name="TabPage2" text="代码创建" type="tabpage"/>
		</div>
	</body>
	</html>
	"""
	renderFaceCat(appPaint, xml)
	tree = findViewByName("Tree", appPaint.views)
	#隐藏列头
	tree.headerHeight = 0
	#显示复选框
	tree.showCheckBox = True

	#创建列
	column = FCGridColumn()
	column.width = 500
	tree.columns.append(column)
		
	#创建根节点
	rootNode = FCTreeNode()
	rootNode.value = "证监会行业类"
	appendTreeNode(tree, rootNode, None)

	#创建子节点
	node1 = FCTreeNode()
	node1.value = "农、林、牧、渔业"
	appendTreeNode(tree, node1, rootNode)

	#创建孙节点
	strs1 = []
	strs1.append("农业")
	strs1.append("林业")
	strs1.append("畜牧业")
	strs1.append("渔业")
	strs1.append("农、林、牧、渔服务业")
	for i in range(0, len(strs1)):
		subNode1 = FCTreeNode()
		subNode1.value = strs1[i]
		appendTreeNode(tree, subNode1, node1)

	#创建子节点
	node2 = FCTreeNode()
	node2.value = "采矿业"
	appendTreeNode(tree, node2, rootNode)

	#创建孙节点
	strs2 = []
	strs2.append("煤炭开采和洗选业")
	strs2.append("石油和天然气开采业")
	strs2.append("黑色金属矿采选业")
	strs2.append("有色金属矿采选业")
	strs2.append("非金属矿采选业")
	strs2.append("开采辅助活动")
	for i in range(0, len(strs2)):
		subNode2 = FCTreeNode()
		subNode2.value = strs2[i]
		appendTreeNode(tree, subNode2, node2)

	#创建子节点
	node3 = FCTreeNode()
	node3.value = "制造业"
	appendTreeNode(tree, node3, rootNode)

	#创建孙节点
	strs3 = []
	strs3.append("农副食品加工业")
	strs3.append("食品制造业")
	strs3.append("酒、饮料和精制茶制造业")
	strs3.append("纺织业")
	strs3.append("纺织服装、服饰业")
	strs3.append("皮革、毛皮、羽毛及其制品和制鞋业")
	strs3.append("木材加工和木、竹、藤、棕、草制品业")
	strs3.append("家具制造业")
	strs3.append("造纸和纸制品业")
	strs3.append("印刷和记录媒介复制业")
	strs3.append("文教、工美、体育和娱乐用品制造业")
	strs3.append("石油加工、炼焦和核燃料加工业")
	strs3.append("化学原料及化学制品制造业")
	strs3.append("医药制造业")
	strs3.append("化学纤维制造业")
	strs3.append("橡胶和塑料制品业")
	strs3.append("非金属矿物制品业")
	strs3.append("黑色金属冶炼和压延加工业")
	strs3.append("有色金属冶炼和压延加工业")
	strs3.append("金属制品业")
	strs3.append("通用设备制造业")
	strs3.append("专用设备制造业")
	strs3.append("汽车制造业")
	strs3.append("铁路、船舶、航空航天和其他运输设备制造业")
	strs3.append("电气机械和器材制造业")
	strs3.append("计算机、通信和其他电子设备制造业")
	strs3.append("仪器仪表制造业")
	strs3.append("其他制造业")
	strs3.append("废弃资源综合利用业")
	for i in range(0, len(strs3)):
		subNode3 = FCTreeNode()
		subNode3.value = strs3[i]
		appendTreeNode(tree, subNode3, node3)

class AppDelegate(NSObject):
	def windowWillClose_(self, notification):
		os._exit(0)
	def applicationDidFinishLaunching_(self, notification):
		appPaint.size = FCSize(900, 770)
		window = NSWindow.alloc().initWithContentRect_styleMask_backing_defer_(
		NSMakeRect(0, 0, appPaint.size.cx, appPaint.size.cy),
		15,
		NSBackingStoreBuffered,
		False
		)
		window.setDelegate_(self)
		window.orderFrontRegardless()
		view = FCUIView.alloc().initWithFrame_(NSMakeRect(0, 0, appPaint.size.cx, appPaint.size.cy))
		view.paint = appPaint
		window.setContentView_(view)
		appPaint.view = view
		loadUI()

app = NSApplication.sharedApplication()
delegate = AppDelegate.alloc().init()
app.setDelegate_(delegate)
app.run()
